#!/usr/bin/perl
#
#

while($line=<STDIN>) {
	chomp($line);
	my(%alignments);
	@alns = split(/\s+/,$line);
	foreach $aln (@alns) {
		($src,$tgt) = split(/-/,$aln);
		push(@{$alignments{$src}},$tgt);
	}

	my(%seen);
	my(@groups);
	foreach $src (sort {$a<=>$b} keys %alignments) {
		if($seen{$src}) {
			next;
		}
#		$group = "$src"."(".scalar(@{$alignments{$src}}).")";
		$group=$src;

		foreach $mSrc (sort {$a<=>$b} keys %alignments) {
			if($src==$mSrc || $seen{$mSrc}) {
				next;
			}

			if(&matchArr($alignments{$src},$alignments{$mSrc})) {
				#$group.="-".$mSrc."(".scalar(@{$alignments{$mSrc}}).")";
				$group.="-".$mSrc;
				$seen{$mSrc}=1;
			}
		}
		push(@groups,$group);
	}

	foreach $group (@groups) 
	{
		if($group=~/-/) 
		{
			print STDERR $group."\n";
			@nodes = split(/-/,$group);
			%hash_count = ();
			foreach $node (@nodes) 
			{
				$count = @{$alignments{$node}};
				$hash_count { $node } = $count;
				print STDERR " ".$node."  ".join('=',@{$alignments{$node}})."\n";
			}
			@array = ();
			$p = 0;
			foreach $key (sort { $hash_count{$a} <=> $hash_count{$b} } keys %hash_count) 
			{
				$array[$p] = $key;
				$p++;
			}
			$num = @array; #  array will contain all the nodes with sorted by the number of values it has.

			for($i = 0; $i < $num; $i++)
			{
				$key = $array[$i];
				@arrs= @{$alignments{$key}};
				for($j = $i+1; $j < $num; $j++)
				{
					@tgts= @{$alignments{$array[$j]}};
					if(&match_array(\@arrs,\@tgts))
					{
						next;
					}
					else
					{
						foreach $arr (@arrs)
						{
#						print STDERR $array[$i]."  -->  ".$array[$j]." is the operation \n";
							@tgts= @{$alignments{$array[$j]}};
							$num_tgt = @tgts;
#						print STDERR join('-->',@tgts)."\n";
							for($l = 0; $l < $num_tgt; $l++)
							{
								if($tgts[$l] == $arr)
								{
#								print STDERR "Removing $j from $tgts[$l] \n";
									splice(@{$alignments{$array[$j]}}, $l, 1);
								}
							}
						}
					}
				}
			}
			print STDERR "----------------------------------TRANSFORMED\n";
			foreach $node (@nodes) 
			{
				print STDERR " ".$node."  ".join('=',@{$alignments{$node}})."\n";
			}
		}
	}
	print STDERR "\n\n";

	foreach $src (sort {$a<=>$b} keys %alignments) {
		foreach $tgt (sort {$a<=>$b} @{$alignments{$src}}) {
			print " $src\-$tgt";
		}
	}
	print "\n";

}

sub matchArr {
	my($arr1,$arr2) = ($_[0],$_[1]);

	if(@$arr1==0 || @$arr2==0) {
		return 0;
	}

	foreach $elem1 (@$arr1) {
		foreach $elem2 (@$arr2) {
			if($elem1==$elem2) {
				return 1;
			}
		}
	}
	return 0;
}

sub match_array
{
	my($arr1,$arr2) = ($_[0],$_[1]);
	if(@$arr1 != @$arr2) 
	{
		return 0;
	}
	if(@$arr1==0 || @$arr2==0) {
		return 0;
	}


	%hash_prod = ();
	%hash_real = ();
	
	foreach $elem1 (@$arr1) 
	{
		$hash_prod{ $elem1 }++;
	}
	foreach $elem2 (@$arr2) 
	{
		$hash_real{ $elem2 }++;
	}
	for my $key ( keys %hash_real )
	{
		if($hash_real{$key} != $hash_prod{$key})
		{
			return 0;
		}
	}
	return 1;
}

